package cn.szzsi.service;

import cn.szzsi._Config;
import cn.szzsi.common.bean.WeixinConfig;
import cn.szzsi.common.kit.CommonUtil;
import cn.szzsi.common.model.BrandStore;
import cn.szzsi.common.model.SiteConfig;
import com.jfinal.kit.HttpKit;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.StrKit;
import com.jfinal.weixin.sdk.api.AccessTokenApi;
import com.jfinal.weixin.sdk.api.ApiConfig;
import com.jfinal.weixin.sdk.api.ApiConfigKit;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 处理门店业务类
 * Created by admin on 2017/4/25.
 */
public class BrandStoreService {
    public static final BrandStoreService bss=new BrandStoreService();


    public String pushToWeiXin(BrandStore bs,String shopId,String business_name){
        Map<String,Object> business=new HashMap();
        Map<String,Object>base_info=new HashMap();
        Map<String,Object> map=new HashMap();
        map.put("sid",bs.getStr("shopId"));
        /*map.put("business_name",bs.getStr("business_name"));
        map.put("branch_name",bs.getStr("branch_name"));*/

        map.put("business_name",business_name);
        map.put("branch_name",bs.getStr("business_name"));
        map.put("province",bs.getStr("province"));
        map.put("city",bs.getStr("city"));
        map.put("district",bs.getStr("district"));
        map.put("address",bs.getStr("address"));
        map.put("telephone",bs.getStr("telephone"));
        map.put("categories",bs.getStr("categories"));
        map.put("offset_type",1);
        map.put("longitude",bs.getBigDecimal("longitude"));
        map.put("latitude",bs.getBigDecimal("latitude"));
        //以下是选填项
        String recommend=bs.getStr("recommend");//推荐
        String special=bs.getStr("special");//特色服务
        String introduction=bs.getStr("introduction");//商户简介
        String open_time=bs.getStr("open_time");//开放时间
        String avg_price=bs.getBigDecimal("avg_price").toString();//人均价格
        if(StrKit.notBlank(recommend)){
            map.put("recommend",recommend);
        }
        if(StrKit.notBlank(special)){
            map.put("special",special);
        }
        if(StrKit.notBlank(introduction)){
            map.put("introduction",introduction);
        }
        if(StrKit.notBlank(open_time)){
            map.put("open_time",open_time);
        }
        if(StrKit.notBlank(avg_price)){
            map.put("avg_price",avg_price);
        }
        base_info.put("base_info",map);
        business.put("business",base_info);
        String access_token= CommonUtil.getAccessToken(shopId);
        String reqUrl="https://api.weixin.qq.com/cgi-bin/poi/addpoi?access_token="+access_token;
        String result= HttpKit.post(reqUrl, JsonKit.toJson(business));
        System.out.println("result--------------"+result);
        JSONObject jo=new JSONObject(result);
        String errmsg=jo.getString("errmsg");
        System.out.println("errmsg-----------------------"+errmsg);
        if("ok".equals(errmsg)){
            bs.set("poiId",jo.get("poi_id")).update();
            bs.set("status",1).update();
            return "ok";
        }else{
            return jo.getString("40001");
        }
    }


    public String getAccessToken(String shopId){
        SiteConfig siteConfig = SiteConfig.getByShopId(shopId);
        WeixinConfig weixinConfig=new WeixinConfig();
        weixinConfig.setAppid(siteConfig.getStr("appid"));
        weixinConfig.setSecret(siteConfig.getStr("secret"));
        ApiConfigKit.setThreadLocalApiConfig(getApiConfig(weixinConfig));
        String accessToken= AccessTokenApi.getAccessToken().getAccessToken();
        return accessToken;
    }

    public ApiConfig getApiConfig(WeixinConfig weixinConfig) {
        ApiConfig ac = new ApiConfig();
        // 配置微信 API 相关常量
        //ac.setToken(PropKit.get("token"));
        ac.setAppId(weixinConfig.getAppid());
        ac.setAppSecret(weixinConfig.getSecret());
        /**
         * 是否对消息进行加密，对应于微信平台的消息加解密方式： 1：true进行加密且必须配置 encodingAesKey
         * 2：false采用明文模式，同时也支持混合模式
         */
        ac.setEncryptMessage(_Config.p.getBoolean("encryptMessage", false));
        ac.setEncodingAesKey(_Config.p.get("encodingAesKey", "setting it in config file"));
        return ac;
    }

}
